class TasksController < ApplicationController
  # GET /tasks
  # GET /tasks.xml
  def index
    @tasks = Task.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tasks }
    end
  end

  # GET /tasks/1
  # GET /tasks/1.xml
  def show
    @task = Task.find(params[:id])

	 @task.logfile = ""
    logfile = "public/test-logs/"+@task.id.to_s+"_log.txt";
    IO.foreach(logfile) {
			|line|
			@task.logfile += line;
	 }
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @task }
    end
  end

  # GET /tasks/new
  # GET /tasks/new.xml
  def new
    @task = Task.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @task }
    end
  end

  # GET /tasks/1/edit
  def edit
    @task = Task.find(params[:id])
  end

  # POST /tasks
  # POST /tasks.xml
  def create
    @task = Task.new(params[:task])

    respond_to do |format|
      if @task.save
        format.html { redirect_to(@task) }
        format.xml  { render :xml => @task, :status => :created, :location => @task }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tasks/1
  # PUT /tasks/1.xml
  def update
    @task = Task.find(params[:id])

    respond_to do |format|
      if @task.update_attributes(params[:task])
        format.html { redirect_to(@task) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.xml
  def destroy
    @task = Task.find(params[:id])
    @task.destroy

    respond_to do |format|
      format.html { redirect_to(tasks_url) }
      format.xml  { head :ok }
    end
  end

  def reset
     @task = Task.find(params[:id])

     respond_to do |format|
     	  @task.update_attributes({"stage"=>0, "interrupt"=>0, "rid"=>0});
        format.html { redirect_to('/tasks') }
        format.xml  { head :ok }
     end
  end

  def schedule
     @task = Task.find(params[:id])

     respond_to do |format|
     	  @task.stage == 0 and @task.update_attributes({"stage"=>1, "interrupt"=>0, "rid"=>0});
        format.html { redirect_to('/tasks') }
        format.xml  { head :ok }
     end
  end

  def stop
     @task = Task.find(params[:id])
     respond_to do |format|
     	  @task.update_attributes({"stage"=>5, "interrupt"=>9});
        format.html { redirect_to('/tasks') }
        format.xml  { head :ok }
     end
  end

  def reset_inside
     @task = Task.find(params[:id])

     respond_to do |format|
     	  @task.update_attributes({"stage"=>0, "interrupt"=>0, "rid"=>0});
        format.html { redirect_to('/tasks/'+@task.id.to_s) }
        format.xml  { head :ok }
     end
  end

  def schedule_inside
     @task = Task.find(params[:id])

     respond_to do |format|
     	  @task.stage == 0 and @task.update_attributes({"stage"=>1, "interrupt"=>0, "rid"=>0});
        format.html { redirect_to('/tasks/'+@task.id.to_s) }
        format.xml  { head :ok }
     end
  end

  def stop_inside
     @task = Task.find(params[:id])
     respond_to do |format|
     	  @task.update_attributes({"stage"=>5, "interrupt"=>9});
        format.html { redirect_to('/tasks/'+@task.id.to_s) }
        format.xml  { head :ok }
     end
  end

end
